package com.sanjishu.enterlab.electricity.sale.controller;

import com.sanjishu.enterlab.electricity.sale.domain.*;
import com.sanjishu.enterlab.electricity.sale.service.*;
import com.sanjishu.enterlab.electricity.sale.shiro.ShiroAdmin;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private NewService newService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyBakService companyBakService;
    @Autowired
    private UserMessageService userMessageService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return "redirect:/redirectIndex";
    }

    @RequestMapping(value = "redirectIndex", method = RequestMethod.GET)
    public String redirectIndex() {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        if (loginUser.getTypes() == User.TYPE_ADMIN) {
            return "redirect:/manage/companyInfo/newCompany";
        } else if (loginUser.getTypes() == User.TYPE_SALE || loginUser.getTypes() == User.TYPE_AGENT) {
            return "redirect:/sale/companyInfo/index";
        } else if (loginUser.getTypes() == User.TYPE_SALE_MANAGER) {
            return "redirect:/saleManage/apply/index";
        } else {
            return "redirect:/index";
        }
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index1(Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<New> newList = newService.lastNews();
        List<CompanyInfo> companyInfoList = companyInfoService.findByUserId(loginUser.getId());
        CompanyInfo companyInfo = null;
        if (companyInfoList != null && companyInfoList.size() > 0) {
            companyInfo = companyInfoList.get(0);
            DetailInfo detailInfo = companyInfoService.findByCompanyId(companyInfo.getId());
            companyInfoList.remove(companyInfo);
            model.addAttribute("companyInfo", companyInfo);
            model.addAttribute("detailInfo", detailInfo);
        }
        List<Message> messageList = userMessageService.findByUserId(loginUser.getId());
        model.addAttribute("messageList", messageList);
        model.addAttribute("newList", newList);
        model.addAttribute("companyInfoList", companyInfoList);
        return "index";
    }

    @RequestMapping(value = "company/{id}", method = RequestMethod.GET)
    public String showCompany(@PathVariable Integer id, Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<New> newList = newService.lastNews();
        CompanyInfo companyInfo = companyInfoService.findById(id);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(companyInfo.getId());
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), id);
        List<Message> messageList = userMessageService.findByUserId(loginUser.getId());
        model.addAttribute("messageList", messageList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        model.addAttribute("newList", newList);
        model.addAttribute("companyInfoList", companyInfoList);
        return "index";
    }


    @PostMapping("login")
    @ResponseBody
    public Response login(String passport, String password) throws Exception {

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(passport, DigestUtils.md5Hex(password));
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return Response.getError("账号或密码不正确!");
        }
        return Response.getSuccess((ShiroAdmin)subject.getPrincipal());
    }

    @GetMapping("login")
    public String loginView() {
        return "login";
    }

    @GetMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }

    @RequestMapping(value = "forgetpassword", method = RequestMethod.GET)
    public String forgetPassword() {
        return "forgetPassword";
    }

    @RequestMapping(value = "sendPWDMessage", method = RequestMethod.POST)
    @ResponseBody
    public Response sendPWDMessage(String phone) {
        try {
            messageService.sendCode(phone);
            return Response.buildSuccess("发送成功");
        } catch (Exception e) {
            return Response.getError(e.getMessage());
        }
    }

    @RequestMapping(value="modifyPwd",method= RequestMethod.POST)
    @ResponseBody
    public Response modifyPwd(String phone, String password, HttpServletRequest request){
        return userService.updatePassword(phone, password);
    }

    @RequestMapping(value="updatePassword",method= RequestMethod.POST)
    @ResponseBody
    public Response updatePassword(String newPassword, HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        return userService.modifyPassword(loginUser.getId(), newPassword);
    }

}
